package com.smap;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.smap.common.Utils;
import com.smap.common.Utils.HttpCallback;
import com.smap.control.HistoryControl;
import com.smap.control.InfoControl;
import com.smap.control.NavigateControl;
import com.smap.control.StatusControl;
import com.smap.control.VectorControl;
import com.smap.core.Map;
import com.smap.core.Marker;
import com.smap.event.MapEvent;
import com.smap.event.MapHandler;
import com.smap.format.GeoJsonFormat;
import com.smap.layer.GoogleLayer;
import com.smap.layer.VectorLayer;
import com.smap.layer.WmsLayer;
import com.smap.vector.Feature;

public class SMapTest extends Composite implements EntryPoint {

	public void onModuleLoad() {
		RootLayoutPanel.get().add(new SMapTest());
	}

	Logger logger = Logger.getLogger("smapshow");
	SMap map;

	public SMapTest() { 
		this.map = createMap();
		this.initWidget(map);
		map.zoomByLonLat(144.96, -37.81, 12);
		logger.setLevel(Level.INFO);
		addBaseLayer();
		addWMSLayer();
		addVectorLayer();
		// addMarkers();
	}

	private SMap createMap() {
		map = new SMap();
		map.addControl(new NavigateControl());
		map.addControl(new StatusControl());
		map.addControl(new HistoryControl());
		map.addHandler(new MapHandler() {
			public void onMapEvent(MapEvent mapEvent) {
				logger.log(Level.FINE, mapEvent.toString());
			}
		}, MapEvent.TYPE);
		return map;
	}

	private void addMarkers() {
		String html = "<img src='flag.png' /><br/>hello!<br/><button>go</button>";
		map.addMarker(new Marker(144.96, -37.81, html).project(map.getProj()));
		map.addMarker(new Marker(144.96, -37.85, html).project(map.getProj()));
	}

	private void addBaseLayer() {
		GoogleLayer baseLayer = new GoogleLayer();
		map.addLayer(baseLayer);
	}

	private void addVectorLayer() {
		final VectorLayer vLayer = new VectorLayer();
		map.addLayer(vLayer);

		Utils.get("./test.geojson", new HttpCallback() {

			@Override
			public void execute(String ret) {
				if (ret != null) {
					GeoJsonFormat f = new GeoJsonFormat();
					List<Feature> fList = f.read(ret);
					for (Feature feature : fList) {
						feature.project(map.getProj());
						vLayer.add(feature);
					}
					vLayer.draw();
				}
			}
		});

		{

			// {
			// Feature feat = new Feature();
			// feat.geom = new Point().add(145.0, -37.81).pack();
			// feat.project(map.getProj());
			// feat.label("I am a Point");
			// vLayer.add(feat);
			// }
			// {
			// Feature feat = new Feature();
			// feat.geom = new LineString().add(145.0, -37.81)
			// .add(145.01, -37.80).add(145.05, -37.83).pack();
			// feat.style.fill_color = "#FFFFFF";
			// feat.style.stroke_color = "#101010";
			// feat.project(map.getProj());
			// feat.label("I am a Line");
			// vLayer.add(feat);
			// }
			// {
			// Feature feat = new Feature();
			// feat.geom = new Polygon().add(145.10, -37.91)
			// .add(145.11, -37.85).add(145.05, -37.83).pack();
			// feat.project(map.getProj());
			// feat.label("I am a mighty Polygon");
			// vLayer.add(feat);
			// }
		}

		// set up control
		VectorControl vControl = new VectorControl(vLayer);
		vControl.activate();
		map.addControl(vControl);

	}

	private void addWMSLayer() {
		String server = "http://demo.geonode.org/geoserver/geonode/wms";
		String layer = "geonode:LGA07aAUST_region";
		final WmsLayer wms = new WmsLayer(server, layer);
		map.addLayer(wms);
		InfoControl info = new InfoControl();
		map.addControl(info);
	}
}
